package samsung.sds.survey.surveyStatistics.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.survey.surveyStatistics.model.QualitySurveyStatisticsResultEntity;
import samsung.sds.survey.surveyStatistics.service.QualitySurveyStatisticsService;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;

/**
 * @author dan5.li
 * @createDate 2016-02-15
 * 数字类型问卷调查统计
 */
@Controller
@RequestMapping("/survey/surveyStatistics/qualitySurveyStatistics")
public class QualitySurveyStatisticsController {

	@Autowired
	private QualitySurveyStatisticsService qualitySurveyStatisticsServiceImpl;

	/**
	 * 根据调查ID获取调查信息
	 * 
	 * @param request
	 * @param survey_Id
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/surveyinfo_sitemesh_frame", method = RequestMethod.GET)
	public String getQualitySurveyInfo(HttpServletRequest request)
			throws ParseException {
		// 调查问卷ID
		String surveyId = request.getParameter("survey_id");
		String id = new String(surveyId);
		Map<String, Object> map = qualitySurveyStatisticsServiceImpl
				.getSurveyMessage(id);
		request.getSession().setAttribute("qualitySurvey", map);
		// 返回数字类型调查统计页面
		return "/survey/surveyStatistics/qualitySurveyStatistics";
	}
	
	/**
	 * 分页获取数字类型投票统计结果
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getStatisticsPageList", method = RequestMethod.POST)
	@ResponseBody
	public PageData<QualitySurveyStatisticsResultEntity> getStatisticsPageList(
			HttpServletRequest request) {
		// 调查问卷ID
		QueryParam param = CommonTools.getQueryParam(request);
		return qualitySurveyStatisticsServiceImpl.getStatisticsPageList(param);
	}

	/**
	 * 分页获取数字类型投票统计结果
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getQuestionContent",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getQuestionContent(String survey_Id) {
		// 获取问题信息
		List<QualitySurveyStatisticsResultEntity> questions = qualitySurveyStatisticsServiceImpl
				.getSurveyQuestion(survey_Id);
		Map<String, Object> questionContent = new HashMap<String, Object>();
		questionContent.put("questions", questions);
		return questionContent;
	};
	
	/**
	 * 导出功能
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/qualitySurveyStatisticsExport",method=RequestMethod.GET)
	public void qualitySurveyStatisticsExport(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 获取调查问卷ID
		String surveyId = new String(request.getParameter("survey_id"));
		// 获取调查问卷信息
		Map<String, Object> map = (Map<String, Object>) request.getSession()
				.getAttribute("qualitySurvey");

		qualitySurveyStatisticsServiceImpl.qualitySurveyStatisticsExport(request,
				response, surveyId, map);
  
	}
}
